package com.example.jussi.recyclerviewdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private List<Weathy> weathyList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        //横向滚动
        /*initWeathy();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);*/
        //瀑布流
        initLengthWeathy();
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        WeathyAdapter adapter = new WeathyAdapter(weathyList);
        recyclerView.setAdapter(adapter);
    }

    private void initWeathy() {
        Weathy sunny = new Weathy("Sunny", R.drawable.weathy_01);
        weathyList.add(sunny);
        Weathy cloud = new Weathy("Cloud", R.drawable.weathy_02);
        weathyList.add(cloud);
        Weathy smallRain = new Weathy("SmallRain", R.drawable.weathy_07);
        weathyList.add(smallRain);
        Weathy minRain = new Weathy("MidRain", R.drawable.weathy_08);
        weathyList.add(minRain);
        Weathy bigRain = new Weathy("BigRain", R.drawable.weathy_09);
        weathyList.add(bigRain);
        Weathy thunderstorm = new Weathy("Thunderstorm", R.drawable.weathy_10);
        weathyList.add(thunderstorm);
        Weathy smallSnow = new Weathy("SmallSnow", R.drawable.weathy_11);
        weathyList.add(smallSnow);
        Weathy midSnow = new Weathy("MidSnow", R.drawable.weathy_12);
        weathyList.add(midSnow);
        Weathy bigSnow = new Weathy("BigSnow", R.drawable.weathy_13);
        weathyList.add(bigSnow);
        Weathy icePao = new Weathy("IcePao", R.drawable.weathy_14);
        weathyList.add(icePao);
    }

    private void initLengthWeathy() {
        for(int i=0; i<2; i++){
            Weathy sunny = new Weathy( getRandomLengthName("Sunny"), R.drawable.weathy_01);
            weathyList.add(sunny);
            Weathy cloud = new Weathy(getRandomLengthName("Cloud"), R.drawable.weathy_02);
            weathyList.add(cloud);
            Weathy smallRain = new Weathy(getRandomLengthName("SmallRain"), R.drawable.weathy_07);
            weathyList.add(smallRain);
            Weathy minRain = new Weathy(getRandomLengthName("MidRain"), R.drawable.weathy_08);
            weathyList.add(minRain);
            Weathy bigRain = new Weathy(getRandomLengthName("BigRain"), R.drawable.weathy_09);
            weathyList.add(bigRain);
            Weathy thunderstorm = new Weathy(getRandomLengthName("Thunderstorm"), R.drawable.weathy_10);
            weathyList.add(thunderstorm);
            Weathy smallSnow = new Weathy(getRandomLengthName("SmallSnow"), R.drawable.weathy_11);
            weathyList.add(smallSnow);
            Weathy midSnow = new Weathy(getRandomLengthName("MidSnow"), R.drawable.weathy_12);
            weathyList.add(midSnow);
            Weathy bigSnow = new Weathy(getRandomLengthName("BigSnow"), R.drawable.weathy_13);
            weathyList.add(bigSnow);
            Weathy icePao = new Weathy(getRandomLengthName("IcePao"), R.drawable.weathy_14);
            weathyList.add(icePao);
        }
    }

    private String getRandomLengthName(String name){
        Random random = new Random();
        int length = random.nextInt(20)+1;
        StringBuilder builder = new StringBuilder();
        for(int i=0; i<length; i++){
            builder.append(name);
        }
        return builder.toString();
    }
}
